package User;

import Base.ResourceTest;
import EntityBuilder.RenterBuilder;
import com.huawei.translate.model.Renter;
import com.huawei.translate.vo.user.RenterDto;
import com.huawei.translate.vo.user.UserRoleType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.UUID;

@RunWith(SpringRunner.class)
public class When_user_login extends ResourceTest {

    private Renter renter;

    @Before
    public void setup() {
        renter = new RenterBuilder(renterRepository)
                .WithAppKey("Test")
                .WithAppSecret(UUID.randomUUID().toString())
                .WithCategory("MicroSoftHubCategory")
                .Build();
    }

    @Test
    public void should_return_bad_request_when_user_login_request_invalid() {

        ResponseEntity<RenterDto> response = this.testRestTemplate.getForEntity("/api/user/login", RenterDto.class);

        Assert.assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
    }

    @Test
    public void should_return_forbidden_when_when_user_invalid() {

        ResponseEntity<RenterDto> response = this.testRestTemplate.getForEntity("/api/user/login?username=invalid&password=invalid", RenterDto.class);

        Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
    }

    @Test
    public void should_return_forbidden_when_when_user_password_invalid() {

        ResponseEntity<RenterDto> response = this.testRestTemplate.getForEntity("/api/user/login?username="+renter.getAppKey()+"&password=invalid", RenterDto.class);

        Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
    }

    @Test
    public void should_return_renter_info_when_user_login_with_valid_request() {

        ResponseEntity<RenterDto> response = this.testRestTemplate.getForEntity("/api/user/login?username="+renter.getAppKey()+"&password="+renter.getAppSecret(), RenterDto.class);

        Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
        Assert.assertEquals(renter.getAppKey(), response.getBody().getAppKey());
        Assert.assertEquals(renter.getAppName(), response.getBody().getAppName());
        Assert.assertEquals(renter.getId(), response.getBody().getId());
        Assert.assertEquals(renter.getCategory(), response.getBody().getCategory());
        Assert.assertEquals(UserRoleType.RENTER.getDescription(), response.getBody().getRole());
    }
}
